﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UmlControlLibrary
{
    public class SpecificMenu
    {
        private static Dictionary<Type, SpecificMenu> specificMenus = new Dictionary<Type, SpecificMenu>();

        public static void Register(Type type, SpecificMenu menu)
        {
            if (type == null)
                type = typeof(object);
            if (!specificMenus.ContainsKey(type))
                specificMenus.Add(type, menu);
            else
                specificMenus[type] = menu;
        }

        public static SpecificMenu GetTypeMenu(Type type)
        {
            if (type == null)
                type = typeof(object);
            if (specificMenus.ContainsKey(type))
                return specificMenus[type];
            else
            {
                Register(type, new SpecificMenu());
                return specificMenus[type];
            }
        }

        public Dictionary<string, Dictionary<string, SpecificMenuItem>> Items
        {
            get;
            set;
        }

        public SpecificMenu()
        {
            Items = new Dictionary<string, Dictionary<string, SpecificMenuItem>>();
        }

        public void AddItem(SpecificMenuItem item)
        {
            Dictionary<string, SpecificMenuItem> items = null;
            if (!Items.ContainsKey(item.Group))
            {
                items = new Dictionary<string, SpecificMenuItem>();
                Items.Add(item.Group, items);
            }
            else
                items = Items[item.Group];

            string itemId = item.Group+"|"+item.Caption;
            if (!items.ContainsKey(itemId))
                items.Add(itemId, item);
        }

        public List<SpecificMenuItem> GetItemGroup(string name)
        {
            if (Items.ContainsKey(name))
                return new List<SpecificMenuItem>(Items[name].Values);
            else
                return null;
        }
    }
}
